package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.enums.ProductEnum;
import com.wing.common.service.ExternalProductService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.model.entity.ProductDetailAttribute;
import com.wing.product.model.request.ProductReviewAddForm;
import com.wing.product.model.request.ProductReviewAuditForm;
import com.wing.product.model.response.ProductReviewSearchVo;
import com.wing.product.model.response.ReviewDetailVo;
import com.wing.product.service.DynamicService;
import com.wing.product.service.ProductDetailService;
import com.wing.res.model.entity.ResProTask;
import com.wing.res.model.entity.ResProductDraw;
import com.wing.res.service.ResProTaskService;
import com.wing.res.service.ResProductDrawService;
import com.wing.sell.dao.OrderProReviewDao;
import com.wing.sell.model.entity.*;
import com.wing.sell.model.request.OrderProReviewForm;
import com.wing.sell.model.response.OrderProReviewVO;
import com.wing.sell.service.*;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 销售订单产品评审表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class OrderProReviewServiceImpl extends ServiceImpl<OrderProReviewDao, OrderProReview> implements OrderProReviewService {

    @Resource
    private CustomerProductCatalogService catalogService;

    @Resource
    private ProductDetailService productDetailService;

    @Resource
    private OrderProService orderProService;

    @Resource
    private OrderProEnclosureService orderProEnclosureService;

    @Autowired
    private DynamicService dynamicService;

    @Resource
    private ResProTaskService resProTaskService;

    @Resource
    private ResProductDrawService resProductDrawService;

    @Resource
    private OrderService orderService;

    private final String IS_RELEVANCE = "0";
    private final String IS_DEFAULT = "1";

    @Override
    public int add(OrderProReviewForm orderProReviewForm) {

        //校验

        OrderProReview orderProReview = new OrderProReview();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(OrderProReview.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderProReviewForm, orderProReview, copyOptions);

        return baseMapper.insert(orderProReview);
    }

    @Override
    public List<OrderProReviewVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<OrderProReviewVO> pageList4app(Page page, Map<String, Object> params) {
        List<OrderProReviewVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public OrderProReviewVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    /**
     * 查询评审任务列表 带分页
     *
     * @param pg
     * @param params
     * @return
     */
    @Override
    public Page<ProductReviewSearchVo> getReviewPageBy(Page<ProductReviewSearchVo> pg, Map<String, Object> params, String processId) {
        List<ProductReviewSearchVo> searchVos = baseMapper.getReviewPageBy(pg, params, processId);
        pg.setRecords(searchVos);
        return pg;
    }


    /**
     * 添加评审任务
     * 22
     *
     * @param form
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addReviewInfoBy(ProductReviewAddForm form) {
        //1.如果是新的产品的话 加入sku中
        //生成一个临时订单id
        String newProductCode = UniqueIdGeneratorUtil.generateUniqueId("CP", "yyyyMMdd", 1000, 9999);
        //创建新产品 才会插入新的产品
//        if (ProductEnum.ReviewUseOrNewEmum.CJXCP.getCode().equals(form.getIsUseOrAdd())) {
//            ProductDetail productDetail = new ProductDetail();
//            productDetail.setCreator(JwtUtils.getLoginUserId());
//            productDetail.setCreatorName(JwtUtils.getLoginUsername());
//            productDetail.setCreateTime(LocalDateTime.now());
//            productDetail.setModifier(JwtUtils.getLoginUserId());
//            productDetail.setModifierName(JwtUtils.getLoginUsername());
//            productDetail.setUpdateTime(LocalDateTime.now());
//            productDetail.setRemark(form.getInRemark());
//            productDetail.setSellStatus("0");
//            productDetail.setSkuCode(form.getInSkuCode());
//            productDetail.setSkuName(form.getInSkuName());
//            productDetail.setSkuSpec(form.getInSkuSpec());
//            productDetail.setProductTypeId(form.getProductTypeId());
//            productDetail.setAssemblyNature(form.getProductNature());
//            productDetail.setUnitId(form.getInSkuUnit());
//            productDetail.setBaseUnit(form.getInUnit());
//            productDetail.setBaseUnitCount(form.getInUnitCount());
//            productDetailService.save(productDetail);
//            productId = productDetail.getId();
//        } else {
//            productId = form.getInSkuCodeHave();
//        }
        QueryWrapper<CustomerProductCatalog> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", form.getCustomerId());
        wrapper.eq("ext_product_id", form.getExtProductId());
        CustomerProductCatalog catalog = this.catalogService.getOne(wrapper);
        //插入客户产品目录表
        catalog.setModifier(JwtUtils.getLoginUserId());
        catalog.setModifierName(JwtUtils.getLoginUsername());
        catalog.setUpdateTime(LocalDateTime.now());
        catalog.setCusProductType(form.getCusProductCategory());
        catalog.setCusProductSpec(form.getCusProductSpec());
        catalog.setCusProductName(form.getCusProductName());
        catalog.setCusProductCode(newProductCode);
        catalog.setCusProductUnit(form.getCusProductUnit());
        catalog.setPlanGivenTime(form.getPlanGivenTime());
        catalog.setQuantity(form.getCusProductCount());
        //包装说明
        catalog.setRemark(form.getRemark());
        catalogService.updateById(catalog);


        //插入评审表
        OrderProReview orderProReview = new OrderProReview();
        orderProReview.setCreator(JwtUtils.getLoginUserId());
        orderProReview.setCreatorName(JwtUtils.getLoginUsername());
        orderProReview.setCreateTime(LocalDateTime.now());
        orderProReview.setModifier(JwtUtils.getLoginUserId());
        orderProReview.setModifierName(JwtUtils.getLoginUsername());
        orderProReview.setUpdateTime(LocalDateTime.now());
        orderProReview.setReviewCatalogId(catalog.getId());
        orderProReview.setReviewProcessId(form.getReviewProcessId());
        orderProReview.setReviewEndTime(form.getReviewEndTime());
        orderProReview.setStatus(OrderProReview.Status.pending);
        orderProReview.setCode(UniqueIdGeneratorUtil.generateUniqueId("PS", "yyyyMMdd", 1000, 9999));
        String code = dynamicService.getDayNextCode("sel_order_pro_review");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String day = simpleDateFormat.format(new Date());
        orderProReview.setDay(Integer.valueOf(day));
        orderProReview.setNo(Integer.valueOf(code));
        orderProReview.setReviewPostId(form.getPostId());
        orderProReview.setReviewPostName(form.getPostName());
        this.save(orderProReview);

        //插入附件表
        OrderProEnclosure file = new OrderProEnclosure();
        file.setCreator(JwtUtils.getLoginUserId());
        file.setCreatorName(JwtUtils.getLoginUsername());
        file.setCreateTime(LocalDateTime.now());
        file.setModifier(JwtUtils.getLoginUserId());
        file.setModifierName(JwtUtils.getLoginUsername());
        file.setUpdateTime(LocalDateTime.now());
        file.setFileUrl(form.getAttachmentPdf());
        file.setFileName(form.getFileName());
        file.setOrderId(catalog.getId());
        file.setTaskId(orderProReview.getId());
        file.setTechnologyRemark(form.getProcessDescription());
        orderProEnclosureService.save(file);
        return 1;
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    @Override
    public ReviewDetailVo getReviewDetailBy(String id) {
        return baseMapper.getReviewDetailBy(id);
    }


    /**
     * 任务信息审核
     *
     * @param form
     * @return
     */
    @Override
    @Transactional
    public JsonResult linkInfo(ProductReviewAuditForm form) {
        //完善评审表
        OrderProReview orderProReview = baseMapper.selectById(form.getId());
        if (ObjectUtil.isEmpty(orderProReview)) {
            return JsonResult.fail("未查询到需要审核的信息");
        }
        OrderProEnclosure one = null;
        Long productId = null;

        //创建新的产品
        if (ProductEnum.ReviewUseOrNewEmum.CJXCP.getCode().equals(form.getIsUseOrAdd())) {
            ProductDetail productDetail = new ProductDetail();
            productDetail.setCreator(JwtUtils.getLoginUserId());
            productDetail.setCreatorName(JwtUtils.getLoginUsername());
            productDetail.setCreateTime(LocalDateTime.now());
            productDetail.setModifier(JwtUtils.getLoginUserId());
            productDetail.setModifierName(JwtUtils.getLoginUsername());
            productDetail.setUpdateTime(LocalDateTime.now());
            productDetail.setRemark(form.getInRemark());
            productDetail.setSellStatus("0");
            productDetail.setSkuCode(form.getInSkuCode());
            productDetail.setSkuName(form.getInSkuName());
            productDetail.setSkuSpec(form.getInSkuSpec());
            productDetail.setProductTypeId(form.getProductTypeId());
            productDetail.setAssemblyNature(form.getProductNature());
            productDetail.setBaseUnit(form.getInUnitId());
            productDetail.setMinPrice(form.getMinPrice());
            productDetail.setSuggestionMinPrice(form.getSuggestionMinPrice());
            productDetail.setMainCalWay(form.getMainCalWay());//主计量方式
            productDetail.setMainUnitId(form.getMainUnitId());//主计量单位
            productDetail.setMainUnitRate(form.getMainUnitRate());//主计量单位换算率
            productDetail.setSubCalWay(form.getSubCalWay());//辅计量方式
            productDetail.setSubUnitId(form.getSubUnitId());//辅计量单位
            productDetail.setSubUnitRate(form.getSubUnitRate());//辅计量单位换算率
            productDetail.setCostCheckType(form.getCostCheckType());//成本核算类型
            productDetail.setCheckStatus(form.getCheckStatus());//是否质检
            productDetail.setBoxStatus(form.getBoxStatus());//是否料箱管理
            productDetailService.save(productDetail);
            productId = productDetail.getId();
        } else {
            //关联内部的产品
            productId = form.getInSkuCodeHave();
        }
        //根据外部产品 查询出外部物料信息
        CustomerProductCatalog byId = catalogService.getById(form.getReviewCatalogId());
        byId.setInSkuCode(productId);
        byId.setAssignNew(form.getIsUseOrAdd());
        catalogService.updateById(byId);
        //如果关联了订单 则需要回填订单信息
        if (orderProReview.getOrderId() != null) {
            Long orderId = orderProReview.getOrderId();
            Long proId = orderProReview.getProId();
            QueryWrapper<OrderPro> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_id", orderId);
            queryWrapper.eq("cus_product_id", proId);
            queryWrapper.eq("status", OrderPro.Status.accepted.getValue());
            OrderPro pro = orderProService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(pro)) {
                pro.setFinalResult(OrderPro.FinalResult.pass);
                pro.setSuggestLowestPrice(form.getSuggestionMinPrice() != null ? form.getSuggestionMinPrice().toString() : null);
                pro.setSuggestPrice(form.getMinPrice() != null ? form.getMinPrice().toString() : null);
                pro.setProductDetailId(productId);
                //回填productId
                pro.setUpdateTime(LocalDateTime.now());
                pro.setModifierName(JwtUtils.getLoginUsername());
                pro.setModifier(JwtUtils.getLoginUserId());
                orderProService.updateById(pro);
            }
            //查询当前订单是否还剩余没有评审处理的订单
            Integer notDoCount = orderProService.getCountNotReviewCount(orderProReview.getOrderId());
            if (notDoCount <= 0) {
                Order byId1 = orderService.getById(orderProReview.getOrderId());
                //设置为待报价
                byId1.setStatus(Order.Status.offer);
                byId1.setUpdateTime(LocalDateTime.now());
                if (ObjectUtil.isNotEmpty(byId1)) {
                    orderService.updateById(byId1);
                }
            }
        } else {
            orderProReview.setProId(productId);
            //更新附件表
            if (!ObjectUtil.isEmpty(byId)) {
                QueryWrapper<OrderProEnclosure> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("order_id", byId.getId());
                queryWrapper.eq("task_id", form.getId());
                one = orderProEnclosureService.getOne(queryWrapper);
                one.setProId(productId);
                orderProEnclosureService.updateById(one);
            }

        }
        orderProReview.setReviewOkTime(LocalDateTime.now());
        orderProReview.setReviewDesc(form.getReviewDesc());
        orderProReview.setStatus(OrderProReview.Status.linked);
        orderProReview.setResult(OrderProReview.Result.pass);
        orderProReview.setUpdateTime(LocalDateTime.now());
        orderProReview.setModifier(JwtUtils.getLoginUserId());
        orderProReview.setModifierName(JwtUtils.getLoginUsername());
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime createTime = orderProReview.getCreateTime();
        Duration duration = Duration.between(createTime, now);
        long hours = duration.toHours();
        // 计算评审时间 小时为单位
        orderProReview.setReviewTime(hours + "");
        orderProReview.setReviewerName(JwtUtils.getLoginUsername());
        //回填评审任务
        boolean b = this.updateById(orderProReview);
        if (b) {
            return JsonResult.success();
        } else {
            return JsonResult.fail("审核失败 ！");
        }
    }

    /**
     * 查询评审记录信息
     *
     * @param pg
     * @param params
     * @param processId
     * @return
     */
    @Override
    public Page<ProductReviewSearchVo> getReviewRecordPageBy(Page<ProductReviewSearchVo> pg, Map<String, Object> params, String processId) {
        List<ProductReviewSearchVo> searchVos = baseMapper.getReviewRecordPageBy(pg, params, processId);
        pg.setRecords(searchVos);
        return pg;
    }

    /**
     * 添加评审记录
     *
     * @param form
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addReviewRecordInfoBy(ProductReviewAddForm form) {
        //生成一个临时订单id
        String newProductCode = UniqueIdGeneratorUtil.generateUniqueId("CP", "yyyyMMdd", 1000, 9999);
        Long productId;
        String productCode;
        //插入产品表
        if (ProductEnum.ReviewUseOrNewEmum.CJXCP.getCode().equals(form.getIsUseOrAdd())) {
            ProductDetail productDetail = new ProductDetail();
            productDetail.setCreator(JwtUtils.getLoginUserId());
            productDetail.setCreatorName(JwtUtils.getLoginUsername());
            productDetail.setCreateTime(LocalDateTime.now());
            productDetail.setModifier(JwtUtils.getLoginUserId());
            productDetail.setModifierName(JwtUtils.getLoginUsername());
            productDetail.setUpdateTime(LocalDateTime.now());
            productDetail.setRemark(form.getInRemark());
            productDetail.setSellStatus("0");
            productDetail.setSkuCode(form.getInSkuCode());
            productDetail.setSkuName(form.getInSkuName());
            productDetail.setSkuSpec(form.getInSkuSpec());
            productDetail.setProductTypeId(form.getProductTypeId());
            productDetail.setAssemblyNature(form.getProductNature());
            productDetail.setBaseUnit(form.getInUnitId());
            productDetail.setMainCalWay(form.getMainCalWay());//主计量方式
            productDetail.setMainUnitId(form.getMainUnitId());//主计量单位
            productDetail.setMainUnitRate(form.getMainUnitRate());//主计量单位换算率
            productDetail.setSubCalWay(form.getSubCalWay());//辅计量方式
            productDetail.setSubUnitId(form.getSubUnitId());//辅计量单位
            productDetail.setSubUnitRate(form.getSubUnitRate());//辅计量单位换算率
            productDetail.setCostCheckType(form.getCostCheckType());//成本核算类型
            productDetail.setCheckStatus(form.getCheckStatus());//是否质检
            productDetail.setBoxStatus(form.getBoxStatus());//是否料箱管理
            productDetailService.save(productDetail);
            productId = productDetail.getId();
            productCode = form.getInSkuCode();
        } else {
            productId = form.getInSkuCodeHave();
            ProductDetail byId = productDetailService.getById(productId);
            productCode = byId.getSkuCode();
        }

        QueryWrapper<CustomerProductCatalog> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", form.getCustomerId());
        wrapper.eq("ext_product_id", form.getExtProductId());
        wrapper.eq("sku_code", form.getCusSkuCode());
        CustomerProductCatalog catalog = this.catalogService.getOne(wrapper);
        //查询外部产品id产品目录表

        catalog.setModifier(JwtUtils.getLoginUserId());
        catalog.setModifierName(JwtUtils.getLoginUsername());
        catalog.setUpdateTime(LocalDateTime.now());
        catalog.setCusProductType(form.getCusProductCategory());
        catalog.setCusProductSpec(form.getCusProductSpec());
        catalog.setCusProductName(form.getCusProductName());
        catalog.setPlanGivenTime(form.getPlanGivenTime());
        catalog.setCusProductCode(newProductCode);
        catalog.setCusProductUnit(form.getCusProductUnit());
        catalog.setInSkuCode(productId);
        catalog.setAssignNew(form.getIsUseOrAdd());
        catalog.setQuantity(form.getCusProductCount());
        //包装说明
        catalog.setRemark(form.getRemark());
        catalogService.updateById(catalog);

        //插入附件表
        OrderProEnclosure file = new OrderProEnclosure();
        file.setCreator(JwtUtils.getLoginUserId());
        file.setCreatorName(JwtUtils.getLoginUsername());
        file.setCreateTime(LocalDateTime.now());
        file.setModifier(JwtUtils.getLoginUserId());
        file.setModifierName(JwtUtils.getLoginUsername());
        file.setUpdateTime(LocalDateTime.now());
        file.setProId(productId);
        file.setFileUrl(form.getAttachmentPdf());
        file.setFileName(form.getFileName());
        file.setOrderId(catalog.getId());
        file.setTechnologyRemark(form.getProcessDescription());
        orderProEnclosureService.save(file);

        OrderProReview orderProReview = new OrderProReview();
        orderProReview.setCreator(JwtUtils.getLoginUserId());
        orderProReview.setCreatorName(JwtUtils.getLoginUsername());
        orderProReview.setCreateTime(LocalDateTime.now());
        orderProReview.setModifier(JwtUtils.getLoginUserId());
        orderProReview.setModifierName(JwtUtils.getLoginUsername());
        orderProReview.setUpdateTime(LocalDateTime.now());
        orderProReview.setProId(productId);
        orderProReview.setOrderId(catalog.getId());
        //设置为已评审
        orderProReview.setStatus(OrderProReview.Status.accepted);
        orderProReview.setCode(UniqueIdGeneratorUtil.generateUniqueId("PS", "yyyyMMdd", 1000, 9999));
        String code = dynamicService.getDayNextCode("sel_order_pro_review");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String day = simpleDateFormat.format(new Date());
        orderProReview.setDay(Integer.valueOf(day));
        orderProReview.setNo(Integer.valueOf(code));
        orderProReview.setReviewPostId(JwtUtils.getPostId());
        orderProReview.setReviewCatalogId(catalog.getId());
        if (ProductEnum.ReviewResultEmum.PSTG.getCode().equals(form.getResult())) {
            orderProReview.setResult(OrderProReview.Result.pass);
            //通过 添加研发任务
            Long taskId = addProTask(form.getCustomerId(), form.getPlanGivenTime(), catalog, productId, productCode, orderProReview.getCode());
            ResProductDraw draw = ResProductDraw.builder().creator(JwtUtils.getLoginUserId()).creatorName(JwtUtils.getLoginUsername())
                    .createTime(LocalDateTime.now()).updateTime(LocalDateTime.now()).modifier(JwtUtils.getLoginUserId())
                    .modifierName(JwtUtils.getLoginUsername()).versionName(UniqueIdGeneratorUtil.generateUniqueId("", "yyyyMMdd", 1000, 9999))
//                    .fileUrl(file.getFileUrl()).drawType(MaintenanceEnum.DrawTypeEnum.CPTZ.getCode())
//                    .name(file.getFileName()).code(UniqueIdGeneratorUtil.generateUniqueId("TZ", "yyyyMMdd", 1000, 9999))
                    .isDefault(IS_DEFAULT)
                    .productDetailId(productId)
                    .isRelevance(IS_RELEVANCE)
                    .taskId(taskId)
                    .build();
            resProductDrawService.save(draw);
        } else {
            orderProReview.setResult(OrderProReview.Result.refuse);
        }
        //设置用时
        orderProReview.setReviewTime("0");
        orderProReview.setReviewOkTime(LocalDateTime.now());
        orderProReview.setReviewEndTime(LocalDateTime.now());
        orderProReview.setReviewerName(JwtUtils.getLoginUsername());
        orderProReview.setReviewDesc(form.getReviewDesc());
        orderProReview.setReviewProcessId(form.getReviewProcessId());
        this.save(orderProReview);
        return 1;
    }

    /**
     * 审核
     *
     * @param productReviewAuditForm 审核入参
     * @return
     */
    @Override
    public JsonResult auditInfoBy(ProductReviewAuditForm productReviewAuditForm) {
        if (productReviewAuditForm == null) {
            return JsonResult.fail("未检查到审核信息");
        }
        Long id = productReviewAuditForm.getId();
        //查询评审任务
        OrderProReview orderProReview = this.getById(id);
        //通过才需要走流程
        if (ProductEnum.ReviewResultEmum.PSTG.getCode().equals(productReviewAuditForm.getStatus())) {
            //通过直接改状态
            orderProReview.setReviewOkTime(LocalDateTime.now());
            orderProReview.setReviewDesc(productReviewAuditForm.getReviewDesc());
            orderProReview.setResult(OrderProReview.Result.pass);
            orderProReview.setUpdateTime(LocalDateTime.now());
            orderProReview.setModifier(JwtUtils.getLoginUserId());
            orderProReview.setModifierName(JwtUtils.getLoginUsername());
            orderProReview.setStatus(OrderProReview.Status.linking);
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime createTime = orderProReview.getCreateTime();
            Duration duration = Duration.between(createTime, now);
            long hours = duration.toHours();
            // 计算评审时间 小时为单位
            orderProReview.setReviewTime(hours + "");
            orderProReview.setReviewerName(JwtUtils.getLoginUsername());
            //回填评审任务
            boolean b = this.updateById(orderProReview);
            if (b) {
                return JsonResult.success();
            } else {
                return JsonResult.fail("审核失败 ！");
            }
        } else {
            //如果有订单  告诉询价订单 我这边审核没过
            if (orderProReview.getOrderId() != null) {
                Long orderId = orderProReview.getOrderId();
                Long proId = orderProReview.getProId();
                QueryWrapper<OrderPro> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("order_id", orderId);
                queryWrapper.eq("cus_product_id", proId);
                queryWrapper.eq("status", OrderPro.Status.accepted.getValue());
                OrderPro pro = orderProService.getOne(queryWrapper);
                if (ObjectUtil.isNotEmpty(pro)) {
                    pro.setFinalResult(OrderPro.FinalResult.fail);
                    pro.setUpdateTime(LocalDateTime.now());
                    pro.setModifierName(JwtUtils.getLoginUsername());
                    pro.setModifier(JwtUtils.getLoginUserId());
                    orderProService.updateById(pro);
                }
            }
            //回填评审表
            orderProReview.setResult(OrderProReview.Result.refuse);
            //修改任务评审表的状态
            orderProReview.setReviewOkTime(LocalDateTime.now());
            orderProReview.setReviewDesc(productReviewAuditForm.getReviewDesc());
            orderProReview.setStatus(OrderProReview.Status.accepted);
            orderProReview.setUpdateTime(LocalDateTime.now());
            orderProReview.setModifier(JwtUtils.getLoginUserId());
            orderProReview.setModifierName(JwtUtils.getLoginUsername());
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime createTime = orderProReview.getCreateTime();
            Duration duration = Duration.between(createTime, now);
            long hours = duration.toHours();
            // 计算评审时间 小时为单位
            orderProReview.setReviewTime(hours + "");
            orderProReview.setReviewerName(JwtUtils.getLoginUsername());
            //回填评审任务
            this.updateById(orderProReview);
            boolean b = this.updateById(orderProReview);
            if (b) {
                return JsonResult.success();
            } else {
                return JsonResult.fail("审核失败 ！");
            }
        }
    }

    /**
     * 添加研发任务
     *
     * @param
     * @param catalog
     * @param productId
     * @param productCode
     */
    private Long addProTask(Long customerId, LocalDateTime planGivenTime, CustomerProductCatalog catalog, Long productId, String productCode, String reviewCode) {
        ResProTask resProTask = ResProTask.builder()
                .creator(JwtUtils.getLoginUserId())
                .creatorName(JwtUtils.getLoginUsername())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .modifier(JwtUtils.getLoginUserId())
                .modifierName(JwtUtils.getLoginUsername())
                .taskNo(UniqueIdGeneratorUtil.generateUniqueId("RW", "yyyyMMdd", 1000, 9999))
                .cusProId(catalog.getId())
                .skuId(productId)
                .skuCode(productCode)
                .customerId(customerId)
                .planGivenTime(planGivenTime)
                .source(MaintenanceEnum.Task_Source_Enum.ZYCP.getCode())
                .status(MaintenanceEnum.Task_Status_Enum.DGH.getCode())
                .reviewCode(reviewCode)
                .remark("来自评审任务!")
                .build();
        resProTaskService.save(resProTask);
        return resProTask.getId();
    }
}
